package com.imelite.qyh.util;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Type;
import java.util.List;

/**
 * json数据互转工具类。使用jackson API。
 */
public class JsonUtil {
    private static final Logger log = LoggerFactory.getLogger(JsonUtil.class);
    private static ObjectMapper jacksonMapper = new ObjectMapper();

    /**
     * @param obj
     * @return
     * @Description 把object转换成JSON格式的字符串
     */
    public static String toJson(Object obj) {
        Writer writer = new StringWriter();
        String result = "";
        try {
            ObjectMapper om = new ObjectMapper();
            om.writeValue(writer, obj);
            result = writer.toString();
            if (log.isDebugEnabled()) {
                System.out.println(result);
            }
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug(e.getMessage(), e);
            }
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                    writer = null;
                } catch (IOException e) {
                    if (log.isDebugEnabled()) {
                        log.debug(e.getMessage(), e);
                    }
                }
            }
        }

        return result;
    }

    /**
     * 把JSON格式的字符串转换成对应的对象实例
     *
     * @param json
     * @param clas
     * @return
     */
    public static <T> T toObject(String json, Class<T> clas) {
        T obj = null;
        try {
            ObjectMapper om = new ObjectMapper();
            om.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
            om.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
            obj = om.readValue(json, clas);
        } catch (JsonParseException e) {
            try {
                //针对非JSON规范时使用的JSON串转换
                GsonBuilder gsonBuilder = new GsonBuilder();
                gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss");
                //注册DateTime转换器
                Gson gson = gsonBuilder.create();
                return gson.fromJson(json, clas);
            } catch (Exception e1) {
                log.error(e.getMessage(), e1);
            }
//            log.error(e.getMessage(), e);
        } catch (JsonMappingException e) {
            log.error(e.getMessage(), e);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }

        return obj;
    }

    /**
     * @param json
     * @param type
     * @return
     * @Title: toObject
     * @Description: 把JSON格式的字符串转换成对应的类型
     * @Created: 2017年4月19日 下午5:38:18
     */
    @SuppressWarnings("unchecked")
    public static final <T> T toObject(String json, Type type) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        return (T) gson.fromJson(json, type);
    }

    /**
     * 将json转换成List方式
     *
     * @param json
     * @param valueType
     * @param <T>
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static final <T> T jsonToList(String json, Class<?>... valueType) {
        JavaType javaType = jacksonMapper.getTypeFactory().constructParametricType(List.class, valueType);
        try {
            return (T) jacksonMapper.readValue(json, javaType);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

